package csel.view;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.border.Border;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;

import csel.model.EquippableItem;
import csel.model.GameCommand;
import csel.model.Model;
import csel.model.Item;
import csel.model.Entity;
import csel.view.Viewport.TrampolineListener;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

public class Splasher extends Viewport implements ActionListener, ImageObserver{
	
	JButton newGame;
	JButton loadGame;
	JButton quitGame;
	BufferedImage backgroundImage;
	JFrame frame;

	TrampolineListener t = new TrampolineListener();
	
	public Splasher(Model m) {
		String path = "/images/splash.jpg";
		
		newGame = new JButton("New Game");
		newGame.setActionCommand("spnew");
		newGame.addActionListener(t);

		loadGame = new JButton("Load Game");
		loadGame.setActionCommand("spload");
		loadGame.addActionListener(t);
		
		quitGame = new JButton("Quit Game");
		quitGame.setActionCommand("spquit");
		quitGame.addActionListener(t);
		
		ImagePanel panel = new ImagePanel(new ImageIcon("images/splash.jpg").getImage());

		frame = new JFrame("Welcome to CSEL");
		frame.setSize(720, 660);
		
		Container contentPane = frame.getContentPane();
        SpringLayout layout = new SpringLayout();
        contentPane.setLayout(layout);

        SpringLayout.Constraints  contentPaneCons = 
    		layout.getConstraints(contentPane);
    	contentPaneCons.setX(Spring.sum(Spring.constant(500),
                              contentPaneCons
                              .getConstraint(SpringLayout.WEST)));
    	contentPaneCons.setY(Spring.sum(Spring.constant(500),
                              contentPaneCons
                              .getConstraint(SpringLayout.NORTH)));
    	
    	//(<label's right edge> + 5, 5).
        SpringLayout.Constraints newGameCons = layout.getConstraints(newGame);
        newGameCons.setX(Spring.constant(100));
        newGameCons.setY(Spring.constant(580));
        
        SpringLayout.Constraints loadGameCons = layout.getConstraints(loadGame);
        loadGameCons.setX(Spring.constant(300));
        loadGameCons.setY(Spring.constant(580));
        
        SpringLayout.Constraints quitGameCons = layout.getConstraints(quitGame);
        quitGameCons.setX(Spring.constant(500));
        quitGameCons.setY(Spring.constant(580));

        //Adjust constraints for the content pane.
        //setContainerSize(contentPane, 5);

        contentPane.add(newGame);
        contentPane.add(loadGame);
        contentPane.add(quitGame);
        
        
        frame.getContentPane().add(panel);

        setVisible(true);
        frame.show();


	}
	

	// paints the image
	public void paintComponent(Graphics g) {
		super.paintComponent(g);        
		g.drawImage(backgroundImage, 0, 0, this);
	}

	
	// makes the screen visible or invisible
	public void setVisible(boolean b) {
		
		if (b) {
			frame.setVisible(true);	
			System.out.println("[VIEW] Splash screen visible!!");
		}
		
		else {
			frame.setVisible(false);
			System.out.println("[VIEW] Splash screen non-visible!!");
		}
			
	}
	
	// destroys the splash screen, never to be seen again.
	public void close()
	{
		System.out.println("[VIEW] Splash closed.");
		frame.dispose();
	}


	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	protected Container getContainer() {
		// TODO Auto-generated method stub
		return null;
	}


	public boolean imageUpdate(Image img, int infoflags, int x, int y,
			int width, int height)
	{
		// TODO Auto-generated method stub
		return false;
	}
	
	public void accept(GameCommand gameCommand)
	{
		gameCommand.execute();
	}


}

class ImagePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private Image img;

	  public ImagePanel(String img) {
	    this(new ImageIcon(img).getImage());
	  }

	  public ImagePanel(Image img) {
	    this.img = img;
	    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
	    setLayout(null);
	  }

	  public void paintComponent(Graphics g) {
	    g.drawImage(img, 0, 0, null);
	  }

	}